package com.example.gss.controller;

import com.example.gss.entity.CourseScore;
import com.example.gss.Service.CourseScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 学业成绩控制器
 */
@RestController
@RequestMapping("/api/course-scores")
public class CourseScoreController {

    @Autowired
    private CourseScoreService scoreService;

    /**
     * 添加成绩记录
     * @param score 成绩记录
     * @return 添加结果
     */
    @PostMapping
    public ResponseEntity<CourseScore> addScore(@RequestBody CourseScore score) {
        CourseScore savedScore = scoreService.addScore(score);
        return new ResponseEntity<>(savedScore, HttpStatus.CREATED);
    }

    /**
     * 获取单个成绩记录
     * @param id 成绩ID
     * @return 成绩记录信息
     */
    @GetMapping("/{id}")
    public ResponseEntity<CourseScore> getScore(@PathVariable String id) {
        CourseScore score = scoreService.getScoreById(id);
        return score != null ?
                ResponseEntity.ok(score) :
                ResponseEntity.notFound().build();
    }

    /**
     * 获取所有成绩记录
     * @return 成绩记录列表
     */
    @GetMapping
    public ResponseEntity<List<CourseScore>> getAllScores() {
        List<CourseScore> scores = scoreService.getAllScores();
        return ResponseEntity.ok(scores);
    }

    /**
     * 根据学生ID获取成绩记录
     * @param studentId 学生ID
     * @return 成绩记录列表
     */
    @GetMapping("/student/{studentId}")
    public ResponseEntity<List<CourseScore>> getScoresByStudentId(@PathVariable String studentId) {
        List<CourseScore> scores = scoreService.getScoresByStudentId(studentId);
        return ResponseEntity.ok(scores);
    }

    /**
     * 根据课程ID获取成绩记录
     * @param courseId 课程ID
     * @return 成绩记录列表
     */
    @GetMapping("/course/{courseId}")
    public ResponseEntity<List<CourseScore>> getScoresByCourseId(@PathVariable String courseId) {
        List<CourseScore> scores = scoreService.getScoresByCourseId(courseId);
        return ResponseEntity.ok(scores);
    }

    /**
     * 更新成绩记录
     * @param id 成绩ID
     * @param score 成绩记录
     * @return 更新结果
     */
    @PutMapping("/{id}")
    public ResponseEntity<CourseScore> updateScore(
            @PathVariable String id, @RequestBody CourseScore score) {
        CourseScore updatedScore = scoreService.updateScore(id, score);
        return updatedScore != null ?
                ResponseEntity.ok(updatedScore) :
                ResponseEntity.notFound().build();
    }

    /**
     * 删除成绩记录
     * @param id 成绩ID
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteScore(@PathVariable String id) {
        scoreService.deleteScore(id);
        return ResponseEntity.noContent().build();
    }

    /**
     * 计算指定学生的平均成绩
     * @param studentId 学生ID
     * @return 平均成绩
     */
    @GetMapping("/student/{studentId}/average-score")
    public ResponseEntity<BigDecimal> calculateAverageScore(@PathVariable String studentId) {
        BigDecimal averageScore = scoreService.calculateAverageScoreByStudentId(studentId);
        return ResponseEntity.ok(averageScore);
    }

    /**
     * 计算指定学生的学位课程平均成绩
     * @param studentId 学生ID
     * @return 学位课程平均成绩
     */
    @GetMapping("/student/{studentId}/degree-course-average-score")
    public ResponseEntity<BigDecimal> calculateDegreeCourseAverageScore(@PathVariable String studentId) {
        BigDecimal degreeCourseAverageScore = scoreService.calculateDegreeCourseAverageScoreByStudentId(studentId);
        return ResponseEntity.ok(degreeCourseAverageScore);
    }

    /**
     * 批量导入成绩
     * @param file Excel文件
     * @return 导入结果
     */
    @PostMapping("/batch-import")
    public ResponseEntity<Map<String, Object>> batchImportScores(@RequestParam("file") MultipartFile file) {
        try {
            Map<String, Object> result = scoreService.batchImportScores(file);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            Map<String, Object> errorResult = Map.of(
                "success", false,
                "message", "批量导入失败：" + e.getMessage()
            );
            return ResponseEntity.badRequest().body(errorResult);
        }
    }
}